﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRO_DOCS.DocumentManagementSystem.ADMIN.WIN.Configurations.Storage
{
    public partial class SavingDirectories : BLL.WIN.Templates.DevxConfigurationUserControlTemplate
    {

        private DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHy _aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY;
        private DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHy ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY
        {
            get { return _aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY; }
            set
            {
                _aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY = value;
                if (_aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY.ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID > 0)
                {
                    txtStorageName.Text = _aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY.ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_NAME;
                    storagePhysicalPathes1.SELECTED_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY = _aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY;
                }
            }
        }
        public SavingDirectories()
        {
            InitializeComponent();
            ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY = new DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHy();
            btnAddNew.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnAddNew_ItemClick);
            btnDeleteItem.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnDeleteItem_ItemClick);
            btnRefresh.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnRefresh_ItemClick);
            btnSaveUpdate.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(btnSaveUpdate_ItemClick);


            try
            {
                RELOAD_ARCHIVE_DEPARTMENTS_STORAGE_HEIRARCHIES();
            }
            catch { }
        }

        void btnSaveUpdate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            dxErrorProvider1.ClearErrors();
            if (!string.IsNullOrEmpty(txtStorageName.Text))
            {

                int? aRCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID = 0;
                if (ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY.ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID > 0) //update
                {
                    aRCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID = ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY.ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID;
                    PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHIESUpdate
                        (ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY.ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID,
                        txtStorageName.Text);

                    gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gcHeirarchyName, txtStorageName.Text);
                }
                else
                {
                    PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHIESInsert
                        (ref aRCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID,
                        txtStorageName.Text);

                    RELOAD_ARCHIVE_DEPARTMENTS_STORAGE_HEIRARCHIES();

                    CLEAR_STORAGE_HEIRARCHY();
                }

                ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY = (from storage in PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHies
                                                          where storage.ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID.Equals(aRCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID.Value)
                                                          select storage).FirstOrDefault();
            }
            else
            {
                dxErrorProvider1.SetError(txtStorageName, BLL.WIN.Properties.ErrorProviderMessages.Default.InputLeftBlank.Replace("{name}", "Role name"));
            }
        }

        void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RELOAD_ARCHIVE_DEPARTMENTS_STORAGE_HEIRARCHIES();
        }

        private void RELOAD_ARCHIVE_DEPARTMENTS_STORAGE_HEIRARCHIES()
        {
            List<DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHy> aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHies = PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHies.ToList();

            gridControl1.DataSource = aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHies;
        }

        void btnDeleteItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY.ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID > 0)
            {
                try
                {
                    PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHIESDelete
                        (ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY.ARCHIVE_DEPARTMENT_STORAGE_HEIRARCHY_ID);

                    gridView1.DeleteRow(gridView1.FocusedRowHandle);
                    CLEAR_STORAGE_HEIRARCHY();
                }
                catch
                {
                    XtraMessageBox.Show("The Storage role cannot be deleted as it is already used in another transaction(s)", "Action denied", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

            }
        }
        private void CLEAR_STORAGE_HEIRARCHY()
        {
            txtStorageName.Text = string.Empty;
            storagePhysicalPathes1.Enabled = false;
            storagePhysicalPathes1.CLEAR_STORAGE_PHYSICAL_PATHES();
        }
        void btnAddNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY = new DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHy();
            CLEAR_STORAGE_HEIRARCHY();
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle > -1)
            {

                ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY = gridView1.GetRow(e.FocusedRowHandle) as DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHy;

                storagePhysicalPathes1.Enabled = true;
            }
        }
    }
}
